﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace DataFileApplication_WPF.Common
{

    //Int类型的数据校验
    public class IntValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
                return new ValidationResult(false, "请输入数字");

            int number;
            if (!int.TryParse(value.ToString(), out number))
                return new ValidationResult(false, "必须为Int类型数值");

            return ValidationResult.ValidResult;
        }
    }

    //Float类型的数据校验
    public class FloatValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
                return new ValidationResult(false, "请输入数字");

            float number;
            if (!float.TryParse(value.ToString(), out number))
                return new ValidationResult(false, "必须为Float类型数值");

            return ValidationResult.ValidResult;
        }
    }

    //Double类型的数据校验
    public class DoubleValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
                return new ValidationResult(false, "请输入数字");

            double number;
            if (!double.TryParse(value.ToString(), out number))
                return new ValidationResult(false, "必须为Double类型数值");

            return ValidationResult.ValidResult;
        }
    }
}
